﻿using Assets.Content.Scripts.DateBase;
using Assets.Content.Scripts.DateBase.AssetBundleLoad;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Assets.Content.Scripts.UI.Start
{
    internal class AdjustPanel : Widget.Widget
    {
        [SerializeField, LabelText("头像图标")]
        private Image _headIcon;

        [SerializeField, LabelText("随机头像按钮")]
        private Button _randomHeadIcon;

        [SerializeField, LabelText("姓名输入框")]
        private InputField _nameInput;

        [SerializeField, LabelText("年龄选择框")]
        private Dropdown _ageInput;

        [SerializeField, LabelText("技能点数")]
        private Text _skillPointText;

        [SerializeField, LabelText("能力列表")]
        private GameObject[] _abilityList;       

        [SerializeField, LabelText("建造")]
        private Button _buildSkill;

        [SerializeField, LabelText("制作")]
        private Button _createSkill;

        [SerializeField, LabelText("缝纫")]
        private Button _sewingSkill;

        [SerializeField, LabelText("科技")]
        private Button _technologySkill;

        [SerializeField, LabelText("确认按钮")]
        private Button _confirm;

        [SerializeField, LabelText("返回按钮")]
        private Button _cancel;

        [SerializeField, LabelText("提示窗口")]
        private GameObject _messageBox;

        private     int      _index = -1;              // 控制头像的增量
        private     Button   _messageBoxConfirmButton; // 提示窗口确认按钮

        // 技能点数
        private     int      _skillPoint;      
        private     int      _headIconValue;
        private     int      _ageIndexValue = 10; 
        private     int      _sexIndexValue;  

        //常规
        private     string   _tempSex;            //性别
        private     string   _tempAge;            //年龄

        // 人体属性值
        private     int      _tempPowerValue;     //力量值
        private     int      _tempAgilityValue;   //敏捷度
        private     int      _tempVigorValue;     //精力值
        private     int      _tempEnduranceValue; //耐力值
        private     int      _tempTechnology;     //说服力
        private     int[]    _tempAbilityValues = new int[4];

        // 能力
        private     int      _tempBuildValue;     //建造
        private     int      _tempCreateValue;    //制作
        private     int      _tempSewingValue;    //缝纫
        private     int      _tempTechnologyValue;//科技

        protected override void Awake()
        {
            base.Awake();

            _messageBoxConfirmButton = _messageBox.transform.GetChild(1).GetComponent<Button>();

            _confirm.onClick.AddListener(OnSelected);
            _cancel.onClick.AddListener(OnDisSelected);
            _randomHeadIcon.onClick.AddListener(OnClickHeadIconButton);
            _ageInput.onValueChanged.AddListener(OnAgeChange);
            _nameInput.onEndEdit.AddListener(OnNameChangeEnd);
            _messageBoxConfirmButton.onClick.AddListener(() => { _messageBox.SetActive(false); });

            _buildSkill.onClick.AddListener(() => OnClickAddBuildSkill(ref _tempBuildValue, _abilityList[0]));
            _createSkill.onClick.AddListener(() => OnClickAddBuildSkill(ref _tempCreateValue, _abilityList[1]));
            _sewingSkill.onClick.AddListener(() => OnClickAddBuildSkill(ref _tempSewingValue, _abilityList[2]));
            _technologySkill.onClick.AddListener(() => OnClickAddBuildSkill(ref _tempTechnologyValue, _abilityList[3]));

            if (_ageInput.value.Equals(0))//初始化
            {
                _tempAge = "青年";
                _tempTechnology = 0;
            }
        }

        protected override void Update()
        {
            base.Update();
            _skillPointText.text = _skillPoint.ToString();
        }

        #region 功能函数

        private void SetSkillPoint()
        {
            _skillPoint = _ageIndexValue + _sexIndexValue + _headIconValue;
        }

        private void SetAbilityValue(int[] addInt, bool isAdd)
        {
            if (isAdd)
                _tempAbilityValues = new[] { addInt[0], addInt[1], addInt[2], addInt[3] };

            //根据不同的年龄段增减属性值
            if (_ageInput.value.Equals(0))
                addInt = new[] { 0, 0, 0, 0 };
            if (_ageInput.value.Equals(1))
                addInt = new[] { 1, 1, 1, 1 };
            if (_ageInput.value.Equals(2))
                addInt = new[] { -2, -2, -2, -2 };

            for (var i = 0; i < 8; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    transform.GetChild(3).GetChild(j).GetChild(0).GetChild(i).GetComponent<Image>().sprite =
                        i < _tempAbilityValues[j] + addInt[j]
                            ? LoadBundlesFromFile.Instance.CommandSprite[9]
                            : LoadBundlesFromFile.Instance.CommandSprite[10];

                    //获得数据
                    if (j.Equals(0))
                        if (i < _tempAbilityValues[j] + addInt[j])
                            _tempPowerValue = _tempAbilityValues[j] + addInt[j];
                    if (j.Equals(1))
                        if (i < _tempAbilityValues[j] + addInt[j])
                            _tempAgilityValue = _tempAbilityValues[j] + addInt[j];
                    if (j.Equals(2))
                        if (i < _tempAbilityValues[j] + addInt[j])
                            _tempVigorValue = _tempAbilityValues[j] + addInt[j];
                    if (j.Equals(3))
                        if (i < _tempAbilityValues[j] + addInt[j])
                            _tempEnduranceValue = _tempAbilityValues[j] + addInt[j];
                }
            }
        }

        private void ResetTheAbility()
        {
            for (var i = 0; i < 4; i++)
            {
                for (var j = 0; j < 8; j++)
                {
                    _abilityList[i].transform.GetChild(0).GetChild(j).GetComponent<Image>().sprite =
                        LoadBundlesFromFile.Instance.CommandSprite[10];
                }
            }

            _tempBuildValue = 0;
            _tempCreateValue = 0;
            _tempSewingValue = 0;
            _tempTechnologyValue = 0;
        }

        #endregion

        #region 点击事件

        private void OnClickAddBuildSkill(ref int action,GameObject listGameObject)
        {
            if (_skillPoint <= 0) return;

            if (action < 8)
            {
                _skillPoint--;
                listGameObject.transform.GetChild(0).GetChild(action++).GetComponent<Image>().sprite =
                    LoadBundlesFromFile.Instance.CommandSprite[9];
            }
        }

        private void OnNameChangeEnd(string name)
        {
            var res = false;
            foreach (var t in name)
                if (t > 127)
                    res = true;

            if (res)
            {
                if (name.Length > 6) 
                {
                    _messageBox.SetActive(true);
                    _messageBox.transform.GetChild(3).GetComponent<Text>().text = "中文名长度不能大于6,请重新输入";
                    _nameInput.text = null;
                }
            }
            else
            {
                if (name.Length > 13)
                {
                    _messageBox.SetActive(true);
                    _messageBox.transform.GetChild(3).GetComponent<Text>().text = "英文名长度不能大于13,请重新输入";
                    _nameInput.text = null;
                }
            }
        }

        private void OnClickHeadIconButton()            //随机头像
        {
            if (_index.Equals(5)) _index = -1;
            _headIcon.sprite = LoadBundlesFromFile.Instance.HeadIconSprite[++_index];

            //男性技能点6  女性技能点5
            _sexIndexValue = _index % 2 == 0 ? 5 : 6;

            var tempValue = new int[4];
            switch (_index)
            {
                            //力量  敏捷  精力  耐力
                case 0:
                    tempValue = new[] {4, 2, 4, 5};
                    _headIconValue = 4; // 技能点数
                    _tempSex = "男";
                    break;
                case 1:
                    tempValue = new[] {3, 3, 5, 4};
                    _headIconValue = 3;
                    _tempSex = "女";
                    break;
                case 2:
                    tempValue = new[] {3, 3, 3, 3};
                    _headIconValue = 2;
                    _tempSex = "男";
                    break;
                case 3:
                    tempValue = new[] {2, 2, 4, 3};
                    _headIconValue = 3;
                    _tempSex = "女";
                    break;
                case 4:
                    tempValue = new[] {2, 5, 4, 3};
                    _headIconValue = 2;
                    _tempSex = "男";
                    break;
                case 5:
                    tempValue = new[] {1, 4, 3, 2};
                    _headIconValue = 1;
                    _tempSex = "女";
                    break;
            }

            SetSkillPoint();
            SetAbilityValue(tempValue, true);
            ResetTheAbility();
        }

        private void OnAgeChange(int value)             //年龄
        {
            if (value.Equals(0))
            {
                _tempAge = "青年";
                _tempTechnology = 0;
            }
            if (value.Equals(1))
            {
                _tempAge = "中年";
                _tempTechnology = 3;
            }
            if (value.Equals(2))
            {
                _tempAge = "老年";
                _tempTechnology = 6;
            }

            _ageIndexValue = value + 10;
            SetSkillPoint();
            SetAbilityValue(null, false);
            ResetTheAbility();
        }

        private void OnSelected()                       //确认按钮
        {
            if (_headIconValue.Equals(0) || _nameInput.text.Length.Equals(0) || !_skillPoint.Equals(0))
            {
                _messageBox.SetActive(true);
                _messageBox.transform.GetChild(3).GetComponent<Text>().text = "请您将属性调整面板中的所有信息填写完整。注意如果您没有将可用的技能点数用尽,进入游戏后这些点数将不能再使用";
                return;
            }

            var tempPlayerData = new PlayerData(_nameInput.text, _tempSex, _tempAge, null, _tempPowerValue,
                _tempAgilityValue, _tempVigorValue, _tempEnduranceValue, 0.5f, 0.5f, 1, _tempTechnology, _tempBuildValue,
                _tempCreateValue, _tempSewingValue, _tempTechnologyValue, 0);

            AccessPlayerDate.SavePlayerDate(tempPlayerData);

            Hide();
            SceneManager.LoadScene("GameScene");
        }

        private void OnDisSelected()                   //返回按钮
        {
            Hide();
            StartManager.Instance.DisPlay(true);
        }

        #endregion  
    }
}